package com.handy.plugin.listener;

import Br.RPGAttribute.Attribute;
import Br.RPGAttribute.Tool.MinMax;
import com.handy.lib.annotation.HandyListener;
import com.handy.lib.core.CollUtil;
import com.handy.lib.core.StrUtil;
import com.handy.lib.util.ItemStackUtil;
import com.handy.playertitle.entity.TitleBuff;
import com.handy.playertitle.event.PlayerTitleBuffEvent;
import com.handy.plugin.RpgPlayerTitle;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Map;

/**
 * buff监听
 *
 * @author handy
 * @since 2.8.4
 */
@HandyListener
public class PlayerTitleBuffEventListener implements Listener {

    /**
     * 玩家buff事件.
     *
     * @param event 事件
     */
    @EventHandler
    public void onPlayerTitleBuffEvent(PlayerTitleBuffEvent event) {
        Player player = event.getPlayer();
        // 判断是否是自己插件的类型
        List<TitleBuff> myPluginBuffType = event.getMyPluginBuffType(RpgPlayerTitle.getInstance().getName());
        if (CollUtil.isEmpty(myPluginBuffType)) {
            return;
        }
        for (TitleBuff titleBuff : myPluginBuffType) {
            List<String> loreList = StrUtil.strToStrList(titleBuff.getBuffContent());
            ItemStack itemStack = ItemStackUtil.getItemStack(Material.DIAMOND_SWORD, "RPG_DIAMOND_SWORD", loreList);
            addAttrToOtherAttr(Attribute.getAttribute(player), Attribute.getAttributeFromOneItem(itemStack, player));
        }
    }

    static Map<Attribute.State, MinMax> addAttrToOtherAttr(Map<Attribute.State, MinMax> a, Map<Attribute.State, MinMax> b) {
        if (b == null) {
            return a;
        } else {
            Attribute.State[] var2 = Attribute.State.values();
            int var3 = var2.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                Attribute.State onestate = var2[var4];
                MinMax geta;
                if (a.containsKey(onestate)) {
                    geta = (MinMax) a.get(onestate);
                } else {
                    geta = new MinMax(0.0D, 0.0D);
                }

                MinMax getb;
                if (b.containsKey(onestate)) {
                    getb = (MinMax) b.get(onestate);
                } else {
                    getb = new MinMax(0.0D, 0.0D);
                }

                MinMax finalnum = geta.addOther(getb);
                a.put(onestate, finalnum);
            }

            return a;
        }
    }

}